<template>
  <!-- 背景 -->
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwrap">
      <!-- 左侧标题 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" width="23px" />
          用户名：{{userInfo.nickName}}
        </li>
        <li>我的积分：{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{cartTotal}}</strong>
        </li>
        <li class="login-btn" @click="changIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { WeixinLoginAPI } from "@/request/api";
export default {
  // watch:{
  //   "$route.path":{
  //     handler(newVal,oldVal){
  //       // console.log("121");
  //       let token=localStorage.getItem("x-auth-token")
  //       // if(token){
  //       //   this.changIisLogined(true)
  //       // }else{
  //       //   this.changIisLogined(false)
  //       // }
  //       this.changIisLogined(Boolean(token))
  //     }
  //   }
  // },
  async created() {
    // console.log(this.$route.query.code);
    let code = this.$route.query.code;
    if (code) {
      let res = await WeixinLoginAPI({
        code: code,
      });
      console.log("res-code", res);
      if (res.code == 0) {
        //提示登录成功
        this.asyncIsShowToast({
          isShow: true,
          title: "微信扫码登录成功",
          type: "success",
        });
        //保存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        //切换购物车
        this.changIisLogined(true);
        //删除地址栏code
        this.$router.push(this.$route.path);
      } else if (res.code == 407) {
        //1提示再用手机号码登录一下
        this.asyncIsShowToast({
          isShow: true,
          title: "微信扫码登录成功，请使用手机号码登录",
          type: "success",
        });
        //2弹出登录模态框
        this.changIsShowLoginModal(true);
        //3.存储uuid
        localStorage.setItem("uuid", res["uuid"]);
      } else if (res.code == 400) {
        //1提示二维码已失效
        this.asyncIsShowToast({
          isShow: true,
          title: "微信二维码失效",
          type: "warning",
        });
        //2弹出登录模态框
        this.changIsShowLoginModal(true);
      }
    } else {
      let token = localStorage.getItem("x-auth-token");
      this.changIisLogined(Boolean(token));
      if(token){
        this.asyncChangeUserInfo();
      }else{
        this.initUserInfo()
      }
    }
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal:state=>state.userInfo.cartTotal
    }),
  },
  methods: {
    ...mapMutations({
      changIsShowLoginModal: "showModel/changIsShowLoginModal",
      changIisLogined: "loginStatus/changIisLogined",
      initUserInfo:"userInfo/initUserInfo"
    }),
    ...mapActions({
      asyncIsShowToast: "showToast/asyncIsShowToast",
      asyncChangeUserInfo:"userInfo/asyncChangeUserInfo"
    }),
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    li {
      cursor: pointer;
      float: left;
      margin-left: 20px;
      text-align: center;
      img {
        border-radius: 13px;
        vertical-align: middle;
      }
      &.login-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
      }
      &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>